<?php

namespace backend\models\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\Car;

/**
 * CarSearch represents the model behind the search form about `backend\models\Car`.
 */
class CarSearch extends Car
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['c_id', 'b_id', 'p_id', 'm_id', 's_id', 'a_id', 'master_id', 'guohu'], 'integer'],
            [['c_name', 'shangpai_time', 'mileage', 'biansu', 'paifang', 'shangpai_place', 'nianjian_time', 'shangye_time', 'car_desc', 'car_desx'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Car::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'c_id' => $this->c_id,
            'b_id' => $this->b_id,
            'p_id' => $this->p_id,
            'm_id' => $this->m_id,
            's_id' => $this->s_id,
            'a_id' => $this->a_id,
            'master_id' => $this->master_id,
            'guohu' => $this->guohu,
        ]);

        $query->andFilterWhere(['like', 'c_name', $this->c_name])
            ->andFilterWhere(['like', 'shangpai_time', $this->shangpai_time])
            ->andFilterWhere(['like', 'mileage', $this->mileage])
            ->andFilterWhere(['like', 'biansu', $this->biansu])
            ->andFilterWhere(['like', 'paifang', $this->paifang])
            ->andFilterWhere(['like', 'shangpai_place', $this->shangpai_place])
            ->andFilterWhere(['like', 'nianjian_time', $this->nianjian_time])
            ->andFilterWhere(['like', 'shangye_time', $this->shangye_time])
            ->andFilterWhere(['like', 'car_desc', $this->car_desc])
            ->andFilterWhere(['like', 'car_desx', $this->car_desx]);

        return $dataProvider;
    }
}
